package com.gai.shop.web.controller;

import com.gai.shop.common.Const;
import com.gai.shop.entity.Page;
import com.gai.shop.entity.Product;
import com.gai.shop.service.ProductService;
import com.gai.shop.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ProductController extends BaseController {
    private final ProductService PS = new ProductServiceImpl();

    /**
     * 按类别查询商品的数据， 分页
     * 需要使用page帮助分页处理
     * 业务提供-->查询所有的数据的总行，需要有类别的id作为条件tid
     *           分页的数据 也是需要类别id作为条件
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String getProListByTid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取参数 分页参数currentPage，类别id==>tid需要获取
        Integer tid = Integer.valueOf(request.getParameter("tid"));
        String currentPage = request.getParameter("currentPage");
        if (currentPage == null){
            currentPage = "1";//当currentPage为null就设置为1
        }
        //帮我们做分页的page实体类已经完成实例化
        Page page = new Page(Integer.valueOf(currentPage),8,tid);
        //使用page作为参数来进行查询分页的数据
        List<Product> productList = PS.selectProductLimitByTid(tid, page);
        request.setAttribute(Const.PRO_LIST,productList);
        request.setAttribute(Const.PAGE,page);
        return Const.FORWARD + "/goodsList.jsp";
    }

    public String getProById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用商品的主键 pid来查询单个商品的详情信息
        String pid = request.getParameter("pid");
        Product proById = PS.selectProductByPid(Integer.valueOf(pid));
        request.setAttribute(Const.PRO,proById);
        return  Const.FORWARD + "/goodsDetail.jsp";
    }
}
